<?php
/**
 * other.mod.php
 * 版本号：1.0
 * 最后修改时间：2009年9月28日 14时10分42秒
 * 作者：狐狸<foxis@qq.com>
 * 功能描述: 网站杂项，其他模块
 */

if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
	var $ShowConfig;
	
	var $CacheConfig;
	
	var $TopicLogic;
	
	var $ID = '';


	function ModuleObject($config)
	{
		$this->MasterObject($config);

		$this->ID = (int) ($this->Post['id'] ? $this->Post['id'] : $this->Get['id']);
		
		Load::logic('topic');
		$this->TopicLogic = new TopicLogic($this);
		
		$this->CacheConfig = ConfigHandler::get('cache');
		
		$this->ShowConfig = ConfigHandler::get('show');
		
		$this->InfoConfig = ConfigHandler::get('web_info');
		
		$this->Execute();
		
	}
		
			
	function Execute()
	{
		ob_start();
		if ('wap' == $this->Code) {
			$this->Wap();
		} elseif ('test' == $this->Code) {
			$this->Test();
		}  elseif ('about' == $this->Code) {
			$this->About();
		} elseif ('contact' == $this->Code) {
			$this->Contact();
		} elseif ('joins' == $this->Code) {
			$this->Joins();
		} elseif ('media' == $this->Code) {
			$this->Media();
		} elseif ('groupdelete' == $this->Code) {
			$this->GroupDelete();
		} elseif ('vip_intro'==$this->Code) {
			$this->VipIntro();
		} elseif ('medal'==$this->Code) {
			$this->Medal();
		} elseif ('notice'==$this->Code) {
			$this->Notice();
		} elseif ('checkmedal'==$this->Code) {
			$this->CheckMedal();
		} elseif ('media_more'==$this->Code) {
			$this->Media_More();
		} else {
			$this->Main();
		}		
		$body=ob_get_clean();
		
		$this->ShowBody($body);
	}
    
    function Main()
    {
        $this->Messager("页面不存在",null);
    }
	
	function Test() 
	{
		exit;	
		
	}

	
		function _Media()
	{	
		
		if($this->Get['ids']){
			$where = " where `id` = {$this->Get['ids']} ";
			$per_page_num = $this->ShowConfig['media_view']['user'];	
			$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}&ids={$this->Get['ids']}" : "");
		}
		else{
			$per_page_num = $this->ShowConfig['media']['user'];	
		}
		
		$sql = "select `id`,`media_name`,`media_count` from `".TABLE_PREFIX."media` {$where} order by `id` desc";
		$query = $this->DatabaseHandler->Query($sql);
		$media_list = array();
		$media_ids = array();
		while ($row = $query->GetRow()) 
		{ 
			$media_ids[$row['id']] = $row['id'];	
			$media_list[] = $row;	
		}
		
				$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."members` where `media_id` in ('".implode("','",$media_ids)."')";
		$query = $this->DatabaseHandler->Query($sql);
		extract($query->GetRow());
	
				
		$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));		
		
		$sql = "select `uid`,`ucuid`,`media_id` ,`nickname` ,`username` ,`face_url`, `face` from `".TABLE_PREFIX."members` where `media_id` in ('".implode("','",$media_ids)."') order by `uid` desc {$page_arr['limit']}";
		$query = $this->DatabaseHandler->Query($sql);
		$member_media = array();
		while ($row = $query->GetRow()) 
		{	
			$row['face'] = face_get($row);
			
			$member_media[] = $row;
		}
		
				$sql = "select  `uid`,`buddyid` from  `".TABLE_PREFIX."buddys` where uid='".MEMBER_ID."' ";
		$query = $this->DatabaseHandler->Query($sql);
		$buddys_set = array();
		while ($row = $query->GetRow()) 
		{
			$buddys_set[] = $row['buddyid'];
		}	
		
		$this->Title = "媒体汇 ";
		include($this->TemplateHandler->Template('media'));
		
	}

    
	function Media() 
	{
				$sql = "select `id`,`media_name`,`media_count` from `".TABLE_PREFIX."media`  order by `order` asc";		
				$query = $this->DatabaseHandler->Query($sql);
		$media_list = array();
		$media_ids = array();
		while ($row = $query->GetRow()) 
		{ 
			$media_ids[$row['id']] = $row['id'];	
			$media_list[] = $row;	
		}
		
				$sql = "select  `uid`,`ucuid`,`media_id` ,`nickname` ,`username`, `face_url` ,`face` from `".TABLE_PREFIX."members` where `media_id` in ('".implode("','",$media_ids)."') ";
		$query = $this->DatabaseHandler->Query($sql);
		$media_user = array();
		while ($row = $query->GetRow()) 
		{	
			$row['face'] = face_get($row);
			
			$media_user[] = $row;
		}
		
		

		
				$sql = "select  `uid`,`buddyid` from  `".TABLE_PREFIX."buddys` where uid='".MEMBER_ID."' ";
		$query = $this->DatabaseHandler->Query($sql);
		$buddys_set = array();
		while ($row = $query->GetRow()) 
		{
			$buddys_set[] = $row['buddyid'];
		}	
		
		$this->Title = "媒体汇 ";
		include($this->TemplateHandler->Template('media'));
	}
	
		function Media_More() 
	{
	
		$ids = (int) $this->Get['ids'];
		
				$sql = "select `id`,`media_name`,`media_count` from `".TABLE_PREFIX."media`  order by `id` desc";		
		$query = $this->DatabaseHandler->Query($sql);
		$media_list = array();
		$media_ids = array();
		while ($row = $query->GetRow()) 
		{ 
			$media_ids[$row['id']] = $row['id'];	
			$media_list[] = $row;	
		}
		
		$per_page_num = 15;	
		$query_link = "index.php?mod=" . ($_GET['mod_original'] ? get_safe_code($_GET['mod_original']) : $this->Module) . ($this->Code ? "&amp;code={$this->Code}&ids={$this->Get['ids']}" : "");
	
				$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."members` where `media_id` = '{$ids}'";
		$query = $this->DatabaseHandler->Query($sql);
		extract($query->GetRow());
	
				$page_arr = page ($total_record,$per_page_num,$query_link,array('return'=>'array',));		
		
		$sql = "select `uid`,`ucuid`,`media_id` ,`nickname` ,`username` ,`face_url`, `face` from `".TABLE_PREFIX."members` where `media_id` = '{$ids}' order by `uid` desc {$page_arr['limit']}";
		$query = $this->DatabaseHandler->Query($sql);
		$member_list = array();
		while ($row = $query->GetRow()) 
		{	
			$row['face'] = face_get($row);
			
			$member_list[] = $row;
		}
		
				$sql = "select  `uid`,`buddyid` from  `".TABLE_PREFIX."buddys` where uid='".MEMBER_ID."' ";
		$query = $this->DatabaseHandler->Query($sql);
		$buddys_set = array();
		while ($row = $query->GetRow()) 
		{
			$buddys_set[] = $row['buddyid'];
		}	
		
		$this->Title = "媒体汇 用户";
		include($this->TemplateHandler->Template('media_more'));
		
		
	}

	  function Wap()
  {		
  	if (!$this->Config['wap']) {
  		$this->Messager("{$this->Config['site_name']}的手机访问功能还未开启",null);
  	}
  	
		$this->Title = "手机访问 {$this->Config['site_name']}";
		$this->MetaKeywords = "手机访问,wap,{$this->Config['site_name']}";
		$this->MetaDescription = $this->Title."，可登录、查看、发微博、评论转发等";
		
		include($this->TemplateHandler->Template('topic_wap'));
		
  }
  
    function About()
  {		
  	$this->Title = "关于我们";
  	include($this->TemplateHandler->Template('topic_about'));
	
  }
  
  function Contact()
  {
		$this->Title = "联系我们";
  	include($this->TemplateHandler->Template('topic_about'));
	}
	
	function Joins()
  {
		$this->Title = "加入我们";
	  include($this->TemplateHandler->Template('topic_about'));  	
	}
	
	
	function VipIntro()
	{
		$this->Title = "{$this->Config['site_name']}身份验证";
		include($this->TemplateHandler->Template('topic_vip'));
	}
	
		function Medal()
	{
		$act_list = array('share'=>'分享到微博',);
		
		$act_list['show'] = array('name'=>'微博秀','link_mod'=>'show','link_code'=>'show',);    

        if($this->Config['qqwb_enable'] && qqwb_init($this->Config))
        {
            $act_list['qqwb'] = 'QQ微博';
        }                
      //  $act_list['imjiqiren'] = 'QQ机器人';        
		if ($this->Config['sina_enable'] && sina_weibo_init($this->Config))
		{
			$act_list['sina'] = '新浪微博';
		}
        if('qqrobot'==$this->Code && !isset($act_list['qqrobot']) && isset($act_list['imjiqiren']))
        {
            $this->Code = 'imjiqiren';
        }
        $act_list['medal'] = array('name'=>'勋章','link_mod'=>'other','link_code'=>'medal',);  
       // $act_list['sms'] = '短信';       
		$act = isset($act_list[$this->Code]) ? $this->Code : 'share';
		
		$uid = MEMBER_ID;
		
				$sql = " select `medal_id` from `".TABLE_PREFIX."members` Where `uid` = {$uid}";
		$query = $this->DatabaseHandler->Query($sql);
		$members=$query->GetRow();
		
		$medalid = explode(",",$members['medal_id']);
		
				$sql = "select * from `".TABLE_PREFIX."medal`";
		$query = $this->DatabaseHandler->Query($sql);
		$medal_list = array();
		while($row = $query->GetRow())
		{   
	
			$medal_list[] = $row;
		}


				$sql = "select * from `".TABLE_PREFIX."user_medal` where `uid` = '".MEMBER_ID."'";
		$query = $this->DatabaseHandler->Query($sql);
		$user_medal = array();
		while($row = $query->GetRow())
		{ 
			$user_medal[] = $row['medalid'];
		}
	
		$this->Title = "{$this->Config['site_name']}勋章";
		include($this->TemplateHandler->Template('topic_medal'));
	}
	
	
		function CheckMedal()
	{ 
		$dateline = time();
		$medalid = (int)$this->Get['mids'];
		
				$sql = " select * from `".TABLE_PREFIX."members` Where  `uid` = ".MEMBER_ID." ";
		$query = $this->DatabaseHandler->Query($sql);
		$members=$query->GetRow();
		
				$sql = " select * from `".TABLE_PREFIX."medal` Where `id` = {$medalid}";
		$query = $this->DatabaseHandler->Query($sql);
		$medal_list=$query->GetRow();
		$medal_value = @unserialize($medal_list['conditions']);
				
		
				if($medal_value['type'] == 'topic')
		{
						$sql = " select * from `".TABLE_PREFIX."topic` Where `type` = 'first' and `uid` = ".MEMBER_ID." order by 'dateline' desc limit 0,1";
			$query = $this->DatabaseHandler->Query($sql);
			$topic_list=$query->GetRow();
	
			$return = $this->_chackmdealday($topic_list['dateline'],$medal_value['day']);
		}
		
				if($medal_value['type'] == 'reply')
		{
			$sql = " select * from `".TABLE_PREFIX."topic` Where `type` = 'reply' and `uid` = ".MEMBER_ID." order by 'dateline' desc limit 0,1";
			$query = $this->DatabaseHandler->Query($sql);
			$reply_list=$query->GetRow();
			
			$return = $this->_chackmdealday($reply_list['dateline'],$medal_value['day']);
			
		}
		
				if ($medal_value['type'] == 'invite') {
			
			$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."invite` Where `uid` = '".MEMBER_ID."' ";
			$query = $this->DatabaseHandler->Query($sql);
			extract($query->GetRow());
			
			if ($medal_value['invite'] > $total_record) {
				$this->Messager("未达成获取勋章的条件",-1);
			} else{
				$return =  true;			
			}
		}
		
		
		
				if($medal_value['type'] == 'tag')
		{ 
			$tag = trim($medal_value['tagname']);
			
			$sql = " select `id`,`name` from `".TABLE_PREFIX."tag` Where `name` = '{$tag}' ";
			$query = $this->DatabaseHandler->Query($sql);
			$tags=$query->GetRow();
			
			if($tags)
			{
				$sql = " select `item_id`,`tag_id` from `".TABLE_PREFIX."topic_tag` Where `tag_id` = {$tags['id']} ";
				$query = $this->DatabaseHandler->Query($sql);
				$topicids = array();
				while($row=$query->GetRow())
				{
					$topicids[$row['item_id']] = $row['item_id'];
				}
			}
			if($topicids)
			{
				$sql = " select `tid`,`uid`,`content` from `".TABLE_PREFIX."topic` where `tid` in ('".implode("','",$topicids)."') and `uid` = '".MEMBER_ID."' limit 0,1";
				$query = $this->DatabaseHandler->Query($sql);
				$topiclist=$query->GetRow();
			}
			if($topiclist){
				$return = 1;
			}
			else{
				$this->Messager("未达成获取勋章的条件",-1);
			}
			
		}	
		
				if($return == 1)
		{  
			$sql = " select * from `".TABLE_PREFIX."user_medal` where `medalid` = '{$medalid}' and `uid` = '".MEMBER_ID."' limit 0,1";
			$query = $this->DatabaseHandler->Query($sql);
			$user_medal=$query->GetRow();
			if($user_medal)
			{
				$this->Messager("已经获得该勋章，不要重复操作",-1);
			}
						$sql = "insert into `".TABLE_PREFIX."user_medal` (`uid`,`nickname`,`medalid`,`dateline`) values ('{$members['uid']}','{$members['nickname']}','{$medalid}','{$dateline}')";
			$query = $this->DatabaseHandler->Query($sql);
			
			
			if(!empty($members['medal_id']))
			{
				         		$sql = "select * from `".TABLE_PREFIX."user_medal` where `uid` = '{$members['uid']}'";
        		$query = $this->DatabaseHandler->Query($sql);
        		$user_medal_id = array();
        		while ($row = $query->GetRow()) {
        			$user_medal_id[] = $row['medalid'];
        		}
        		
        		$user_medal_id = implode(",",$user_medal_id);
			}
			
			$user_medal = $user_medal_id ? $user_medal_id : $medalid;

		
						$sql = "update `".TABLE_PREFIX."members` set  `medal_id`='{$user_medal}'  where `uid` = ".MEMBER_ID."";	
			$update = $this->DatabaseHandler->Query($sql);
			
						$sql = "update `".TABLE_PREFIX."medal` set  `medal_count`=`medal_count`+1  where `id` = '{$medalid}'";	
			$this->DatabaseHandler->Query($sql);
			
			
			$this->Messager("成功点亮",'index.php?mod=other&code=medal');
		}		
		else
		{
			$this->Messager("未达成获取勋章的条件",-1);
		}	
	
	}
	
		function _chackmdealday($date_time=0,$chackday=0)
	{
			
			for($i=1; $i <= $chackday;$i++)
			{ 
				
				$start_time = time() - (86400 * $i);
				$endtime = $start_time + 86400 ;
		
			
				$start_time = date('Y-m-d',$start_time);
		
				$start_time = strtotime($start_time);
				if($endtime <= time())
				{ 
					$endtime = date('Y-m-d',$endtime);
					$endtime = strtotime($endtime);	
					$over_date = "and `dateline` <= '{$endtime}'";
				}
				
				
				$sql = " select `dateline` from `".TABLE_PREFIX."topic` Where `dateline` >= '{$start_time}' {$over_date} and `type` = 'first' and `uid` = ".MEMBER_ID." order by 'dateline' desc limit 0,1";
				$query = $this->DatabaseHandler->Query($sql);
				$topic_list = $query->GetRow();
		
				$chack_time .= date('Ymd',$topic_list['dateline']).'|';
				
			}
			$arr_time = explode('|',$chack_time);
			$arr_time = array_filter($arr_time);
			$chack_endtime = date('Ymd',$endtime);
						
			if($chackday > 1)
		  {	
	  		 for($i=0; $i < count($arr_time)-1;$i++)
			   { 
		        if(is_numeric($arr_time[$i]))
		        {
		            if($arr_time[$i]-$arr_time[$i+1]!==1)  
		            { 
		            	$is_lianxu = 'false';
		            	break;	
		            }
		            else
		            { 
		            	$is_lianxu = 'true';
		            }
					  }  
				 }
					 
				 if($is_lianxu == 'true')
				 {
				 		return true;	
				 }
				 else
				 {
				 		$this->Messager("未达成获取勋章的条件",-1);
				 }
					 
		  }
		  elseif($chack_endtime - $chack_time == 1)
		  { 
		  	return 1;	
		  }
		  else
		  {
		  	$this->Messager("未达成获取勋章的条件",-1);
		  }
   } 

	  function GroupDelete()
	{
		$gid = (int) $this->Get['gid'];
		
		$sql = "select `id`,`uid` from `".TABLE_PREFIX."group` where `id` ='{$gid}'";
		$query = $this->DatabaseHandler->Query($sql);
		$user_group = $query->GetRow();
			
		if($user_group['uid'] != MEMBER_ID)
		{
		  $this->Messager('分组不存在','index.php',0);
		}	
	
		$sql = "delete from `".TABLE_PREFIX."group` where `id`='{$gid}' and `uid` =".MEMBER_ID;
	  $this->DatabaseHandler->Query($sql);		
	  
	  $sql = "delete from `".TABLE_PREFIX."groupfields` where `gid`='{$gid}'";
	  $this->DatabaseHandler->Query($sql);	
	
		$this->Messager(NULL,'index.php?mod='.MEMBER_NAME.'&code=follow',0);
	}
	

	
	  function Notice()
  {
  	$ids = $this->Get['ids'];
  	
  	if($ids)
  	{
	  	$sql="Select * From ".TABLE_PREFIX.'notice'." Where id = '{$ids}' ";
			$query = $this->DatabaseHandler->Query($sql);
			$view_notice=$query->GetRow();
			
			$title		 =  $view_notice['title'];
			$content  =  $view_notice['content'];
			$dateline =  my_date_format2($view_notice['dateline']);
			
		}
		
			$sql="select `id`,`title` from ".TABLE_PREFIX.'notice'." order by `dateline` desc limit 5 ";
			$query = $this->DatabaseHandler->Query($sql);
			$list_notice = array();
			while ($row = $query->GetRow())
			{	
				
				$row['titles'] 	= cutstr($row['title'],26);
				$list_notice[] 	= $row;
			}
		
		$this->Title = "{$view_notice['title']}";
        if(!$this->Title) $this->Title = '网站公告';
		include($this->TemplateHandler->Template('view_notice'));
	
  }
	
}

?>
